package flow.util;

import flow.model.FlowBusiness;
import flow.model.FlowInstance;
import flow.service.FlowBusinessService;
import flow.service.IFlowService;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.system.RuntimeData;
import m.system.exception.MException;
import m.system.util.ClassUtil;
import m.system.util.StringUtil;
import manage.model.AdminLogin;

import java.sql.SQLException;
/**
 * 流程功能调用util
 * @author Administrator
 *
 */
public class FlowUtil {

	/**
	 * 反射业务服务类
	 *  busiService
	 * 
	 *  MException
	 */
	public static IFlowService getService(String busiService) throws MException {
		if(StringUtil.isSpace(busiService)) throw new MException(FlowUtil.class, "业务服务类不能为空");
		return ClassUtil.newInstance(busiService);
	}
	/**
	 * 获取流程标识
	 *  busiService
	 * 
	 *  MException
	 */
	public static String getFlowId(String busiService) throws MException {
		return getService(busiService).getFlowIdentity();
	}
	/**
	 * 获取业务的流程实例
	 *  busiService
	 *  busiOid
	 * 
	 *  MException
	 *  SQLException
	 */
	public static FlowInstance getInstance4Business(String busiService,String busiOid) throws MException, SQLException {
		String flowId=getFlowId(busiService);
		return ModelQueryList.getModel(FlowInstance.class, 
			new String[] {"*","currentSection.*","flowDefine.*","flowDefine.startSection.*"}, 
			QueryCondition.and(
				QueryCondition.eq("flowDefine.identity", flowId),
				QueryCondition.eq("business.oid", busiOid))
		);
		
	}
}
